package com.erbao.dp.clone;

class Person implements Cloneable {
    int age = 18;
    int score = 100;

    Location loc = new Location(new String("BeiJing"), 888);

    @Override
    protected Person clone() throws CloneNotSupportedException {
        Person p = (Person) super.clone();
        p.loc = p.loc.clone();
        return p;

    }

    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                ", score=" + score +
                ", loc=" + loc +
                '}';
    }
}

class Location implements Cloneable {
    String street;
    int roomNo;

    public Location(String street, int roomNo) {
        this.street = street;
        this.roomNo = roomNo;
    }

    @Override
    protected Location clone() throws CloneNotSupportedException {
        return (Location) super.clone();
    }

    @Override
    public String toString() {
        return "Location{" +
                "street='" + street + '\'' +
                ", roomNo=" + roomNo +
                '}';
    }
}
/**
 * @author CaoBaoYin
 * @since 2021/10/14 7:41
 */
public class Main {

    public static void main(String[] args) throws CloneNotSupportedException {
        Person p1 = new Person();
        Person p2 = p1.clone();

        System.out.println(p1);
        System.out.println(p2);
        p1.age = 12; p1.score=99; p1.loc.roomNo=666; p1.loc.street="puyang";
        System.out.println(p1);
        System.out.println(p2);
    }

}
